package com.project2.controller;

import com.project2.bean.ResultVo;
import com.project2.bean.bo.AdminConfigBo;
import com.project2.bean.po.MarketSystem;
import com.project2.service.ConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @Author: MH
 * @Date: 2022-08-12 17:36
 * @Description: 配置管理, 四个配置管理所需要的数据都被封装在同一个map数据中,
 *                所以可以直接使用同一实现方法
 */
@RestController
@RequestMapping("admin/config")
public class AdminConfigController {
    @Autowired
    ConfigService configService;

    @GetMapping("mall")
    @RequiresPermissions("admin:config:mall:list")
    public ResultVo getMallConfigs(MarketSystem marketSystem) {
        HashMap<String, String> configs = configService.getConfigs(marketSystem);
        ResultVo ok = ResultVo.ok(configs);
        return ok;
    }

    @GetMapping("express")
    @RequiresPermissions("admin:config:express:list")
    public ResultVo getExpressConfigs(MarketSystem marketSystem) {
        HashMap<String, String> configs = configService.getConfigs(marketSystem);
        ResultVo ok = ResultVo.ok(configs);
        return ok;
    }

    @GetMapping("order")
    @RequiresPermissions("admin:config:order:list")
    public ResultVo getOrderConfigs(MarketSystem marketSystem) {
        HashMap<String, String> configs = configService.getConfigs(marketSystem);
        ResultVo ok = ResultVo.ok(configs);
        return ok;
    }

    @GetMapping("wx")
    @RequiresPermissions("admin:config:wx:list")
    public ResultVo getWxConfigs(MarketSystem marketSystem) {
        HashMap<String, String> configs = configService.getConfigs(marketSystem);
        ResultVo ok = ResultVo.ok(configs);
        return ok;
    }

    @PostMapping("mall")
    @RequiresPermissions("admin:config:mall:updateConfigs")
    public ResultVo updateMallConfig(@RequestBody AdminConfigBo adminConfigBo) {
        configService.updateMallConfig(adminConfigBo);
        return ResultVo.ok();
    }

    @PostMapping("express")
    @RequiresPermissions("admin:config:express:updateConfigs")
    public ResultVo updateExpressConfig(@RequestBody AdminConfigBo adminConfigBo) {
        configService.updateMallConfig(adminConfigBo);
        return ResultVo.ok();
    }

    @PostMapping("order")
    @RequiresPermissions("admin:config:order:updateConfigs")
    public ResultVo updateOrderConfig(@RequestBody AdminConfigBo adminConfigBo) {
        configService.updateMallConfig(adminConfigBo);
        return ResultVo.ok();
    }

    @PostMapping("wx")
    @RequiresPermissions("admin:config:wx:updateConfigs")
    public ResultVo updateWxConfig(@RequestBody AdminConfigBo adminConfigBo) {
        configService.updateMallConfig(adminConfigBo);
        return ResultVo.ok();
    }
}
